package com.wmapp.sender;

import java.security.Security;
import java.util.Date;
import java.util.Properties;

import javax.mail.Authenticator;
import javax.mail.Message;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.util.StringUtils;

import com.sun.net.ssl.internal.ssl.Provider;
import com.wmapp.domain.Email;

public class EmailSender implements Runnable {
	private static final Log log=LogFactory.getLog(EmailSender.class);
	
	private Email email;
	
	public EmailSender(Email email) {
		this.email=email;
	}
	
	@Override
	public void run() {
		send(email);
	}
	
	private void send(Email email) {
		String smtpServer,from,to,subject,body;
		final String account,pwd;
		boolean verify;
		
		if (email == null
				|| !StringUtils.hasText(smtpServer = email.getSmtpServer())
				|| !StringUtils.hasText(to = email.getTo())
				|| !StringUtils.hasText(from = email.getFrom())
				|| !StringUtils.hasText(subject = email.getSubject()))
			return;
		
		body=email.getBody();
		account=email.getAccount();
		pwd=email.getPwd();
		verify=email.isVerify();
		
		Authenticator authenticator=null;
		try {
			Properties props = System.getProperties();
			props.setProperty("mail.smtp.host", smtpServer);
			
			if (smtpServer.indexOf("gmail") != -1) {// gmail,gmail use ssl
				Security.addProvider(new Provider());
				props.setProperty("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
				props.setProperty("mail.smtp.socketFactory.fallback", "false");
				props.setProperty("mail.smtp.port", "465");
				props.setProperty("mail.smtp.socketFactory.port", "465");
				props.setProperty("mail.smtp.auth", "true");
				
				authenticator=new Authenticator() {
					@Override
					protected PasswordAuthentication getPasswordAuthentication() {
						return new PasswordAuthentication(account, pwd);
					}
				};
			} else {// others
				if(verify){
					props.setProperty("mail.smtp.auth", "true");
					
					authenticator=new Authenticator() {
						@Override
						protected PasswordAuthentication getPasswordAuthentication() {
							return new PasswordAuthentication(account, pwd);
						}
					};
				}
			}
			
			Session session = Session.getDefaultInstance(props, authenticator);
			Message msg = new MimeMessage(session);
			msg.setFrom(new InternetAddress(from));
			msg.setRecipients(Message.RecipientType.TO,InternetAddress.parse(to, false));
			msg.setSubject(subject);
			msg.setText(body);
			msg.setSentDate(new Date());
			Transport.send(msg);
			log.info("-----------------Email send successful----------------");
		} catch (Exception ex) {
			log.error(ex.getMessage(), ex);
		}
	}
	
	// Email Sender test
	public static void main(String[] args){
		Email email=new Email();
		email.setFrom("deng.liang.2008@gmail.com");
		email.setAccount("deng.liang.2008");
		email.setPwd("XXX");
		email.setSmtpServer("smtp.gmail.com");
		email.setTo("deng.liang.2008@gmail.com");
		email.setSubject("title...");
		email.setBody("hello");
		
		EmailSender sender = new EmailSender(email);
		
		new Thread(sender).start();
	}
	
}
